package com.example.imtoken.modules.private_import.fragment.code

import android.view.View
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseFragment
import com.example.imtoken.core.utils.MakeQRCodeUtil
import com.example.imtoken.databinding.CodeFragmentBinding

class CodeFragment : BaseFragment<CodeViewModel, CodeFragmentBinding>(CodeViewModel::class.java) {

    override fun initLayoutId(): Int {
        return R.layout.code_fragment
    }

    override fun init() {
        initObserve()
        initClick()
    }

    private fun initClick() {
        dataBinding.tvShowQrCode.setOnClickListener {
            dataBinding.llShowQrCodeTips.visibility = View.GONE
            dataBinding.ivQrCode.visibility = View.VISIBLE
        }
    }

    private fun initObserve() {
        viewModel.value.observe(this) {
            if (it.isNotEmpty()) createQRCode(it)
        }
    }

    private fun createQRCode(content: String?) {
        val bitmap = MakeQRCodeUtil.makeQRImage(
            content,
            ConvertUtils.sp2px(300f),
            ConvertUtils.sp2px(300f)
        )
        dataBinding.ivQrCode.setImageBitmap(bitmap)
    }


    fun setPrivateData(value: String?) {
        viewModel.setValue(value)
    }

}